package com.credithc.customers.manager.service.impl;

import com.credithc.baseserv.core.codes.CoreCodes;
import com.credithc.baseserv.core.exception.BsException;
import com.credithc.baseserv.core.service.impl.BaseServiceImpl;
import com.credithc.baseserv.pageHelper.PageInfo;
import com.credithc.customers.base.codes.CustomerCodes;
import com.credithc.customers.base.exception.CustomerException;
import com.credithc.customers.base.mapper.UnifySmsMapper;
import com.credithc.customers.base.po.UnifySmsCodeFlowPo;
import com.credithc.customers.base.ro.SmsSendFlowRo;
import com.credithc.customers.base.utils.ClassUtils;
import com.credithc.customers.base.utils.LocalDateUtils;
import com.credithc.customers.base.vo.SmsSendFlowVo;
import com.credithc.customers.manager.service.SmsFlowService;
import com.credithc.customers.manager.utils.StatsUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Title: 业务监控处理
 * @Author: fby
 * @Date: 2020/03/24 10:21
 * @Description: com.credithc.customers.manager.service.impl
 * @Version: V1.0.0
 */
@Service
@Slf4j
public class SmsFlowServiceImpl extends BaseServiceImpl<UnifySmsCodeFlowPo, SmsSendFlowRo, SmsSendFlowVo> implements SmsFlowService {

    @Autowired
    private UnifySmsMapper unifySmsMapper;

    @Override
    public PageInfo<SmsSendFlowVo> getSmsSendFlow(SmsSendFlowRo ro) {
        try {
            Integer pageNo = ro.getPageNo();
            Integer pageSize = ro.getPageSize();
            ro.setStartPage(pageSize * (pageNo - 1));
            int count = unifySmsMapper.getListCount(ro);
            List<SmsSendFlowVo> list = unifySmsMapper.getList(ro);
            List<SmsSendFlowVo> vos = new ArrayList<>();
            if (count > 0) {
                for (SmsSendFlowVo po : list) {
                    SmsSendFlowVo vo = new SmsSendFlowVo();
                    BeanUtils.copyProperties(po, vo);
                    if (po.getCreateTime() != null) {
                        vo.setCreateTime(StatsUtils.timestamp2Stirng(Timestamp.valueOf(po.getCreateTime())));
                    }
                    //处理状态
                    if (po.getStatus() != null) {
                        if ("1".equals(po.getStatus())) {
                            vo.setStatus("成功");
                        } else {
                            vo.setStatus("失败");
                        }
                    }
                    vos.add(vo);
                }
            }
            return new PageInfo(vos, count, pageNo, pageSize);
        } catch (DataAccessException e) {
            log.error("查询短信发送流水报错:" + ClassUtils.getClassName() + "--" + ClassUtils.getMethodName() + "--" + e.getMessage());
            throw new CustomerException(CustomerCodes.DATABASE_ERROR_QUERY);
        }
    }

    @Override
    public void deleteLockSts(String phone, String subSystemSign) {
        try {
            String startTime = LocalDateUtils.format(LocalDate.now(), "yyyy-MM-dd");
            String endTime = LocalDateUtils.format(LocalDate.now().plusDays(1), "yyyy-MM-dd");
            //查询当天锁定所有记录
            Example example = new Example(UnifySmsCodeFlowPo.class);
            Example.Criteria criteria = example.createCriteria();
            example.setOrderByClause("create_time desc");
            if (StringUtils.isNotEmpty(phone)) {
                criteria.andEqualTo("phone", phone);
            } else {
                throw new BsException(CoreCodes.CORE_SYS_ERROR, "手机号不能为空");
            }
            if (StringUtils.isNotEmpty(subSystemSign)) {
                criteria.andEqualTo("subSystemSign", subSystemSign);
            }
            criteria.andBetween("createTime", startTime, endTime);
            List<UnifySmsCodeFlowPo> list = unifySmsMapper.selectByExample(example);
            //批量删除锁定记录信息
            log.info("手机解锁list=" + list);
            List<Integer> ids = list.stream().map(UnifySmsCodeFlowPo -> UnifySmsCodeFlowPo.getId()).collect(Collectors.toList());
            log.info("手机解锁ids=" + ids);
            Example example1 = new Example(UnifySmsCodeFlowPo.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andIn("id", ids);
            int result = unifySmsMapper.deleteByExample(example1);
            if (result == 0) {
                throw new BsException(CoreCodes.CORE_SYS_ERROR, "手机解锁批量删除失败");
            }
        } catch (Exception e) {
            log.error("手机解锁失败:" + ClassUtils.getClassName() + "--" + ClassUtils.getMethodName() + "--" + e.getMessage());
            throw new BsException(CoreCodes.CORE_SYS_ERROR,"解锁失败");
        }
    }

}
